﻿using System;
using System.Collections.Generic;
using System.IO.Ports;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using Comm;
using Kinlo.Drv.Drv;
using Kinlo.Drv.Drv.RJShortCircuitEnum;
using Kinlo.Drv.Drv.RJShortCirrcuitResult;

namespace Kinlo.Drv.Machine
{
    public class ShortCircuitMachine
    {
        public bool IsOpen { get { return ShortCircuit.IsOpen; } }
        private RJShortCircuitSerialPort ShortCircuit { get; set; }
        public ShortCircuitMachine(string PortName,int baudRate,Parity parity,int dataBits,StopBits stopBits,int timeOut)
        {
            ShortCircuit = new RJShortCircuitSerialPort(PortName, baudRate, parity, dataBits, stopBits,timeOut);
        }

        /// <summary>
        /// 连接短路测试仪
        /// </summary>
        /// <returns>连接成功返回true;失败返回false</returns>
        public bool Conncet()
        {
            Result result = ShortCircuit.Connect();
            Logger.Info($"短路测试仪连接{(result.IsSuccess ? "成功" : "失败")} 信息:{result.ErroMessage}");
            return result.IsSuccess;
        }

        /// <summary>
        /// 断开短路测试仪
        /// </summary>
        /// <returns></returns>
        public bool DisConncet()
        {
            Result result = ShortCircuit.DisConnect();
            Logger.Info($"短路测试仪断开连接{(result.IsSuccess ? "成功" : "失败")} 信息:{result.ErroMessage}");
            return result.IsSuccess;
        }

        /// <summary>
        /// 启动测试
        /// </summary>
        /// <param name="localID">从机地址</param>
        /// <returns>返回启动结果</returns>
        public Result Start(byte localID)
        {
            return ShortCircuit.StartTest(localID);
        }

        /// <summary>
        /// 获取机型RJ6902B 命令0x11的测试结果
        /// </summary>
        /// <param name="localID">从机地址</param>
        /// <returns>返回结果对象</returns>
        public Result<RJ6902R0X59> GetTestDataAndResult(byte localID)
        {
            return ShortCircuit.GetTestDataAndResult(localID);
        }
        public Result<RJ6902R0X6B> GetWaveInfo(byte localID)
        {
            return ShortCircuit.GetWaveInfo(localID);
        }

        /// <summary>
        /// 获取机型RJ6902B 命令0x12的测试结果
        /// </summary>
        /// <param name="localID">从机地址</param>
        /// <returns>返回测试结果</returns>
        public Result<bool?> GetTestResult(byte localID)
        {
            return ShortCircuit.GetTestResult(localID);
        }

    }
}
